package com.bjqh.edu.UI.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.User;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Index.PrivateClassActivity;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.AcademicPlanningActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.CoursePurchaseActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.StuCumulativeClassActivity;
import com.bjqh.edu.UI.Activity.TeacherPersonal.CumulativeClassActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MyGradeActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MySettingActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MyTeacherActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.OrderActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.PersonalInformationActivity;
import com.bjqh.edu.Widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/9/6.
 */

public class MineFragment extends Fragment {
    public View mViewRoot;
    Unbinder unbinder;
    @BindView(R.id.cv)
    CircleImageView ivFloatHeader;
    @BindView(R.id.tv_floatName)
    TextView tvFloatName;
    @BindView(R.id.tv_my_phone)
    TextView tvMyPhone;
    @BindView(R.id.ll_cumulative)
    LinearLayout llCumulative;
    @BindView(R.id.course_purchase)
    LinearLayout coursePurchase;
    @BindView(R.id.ll_top)
    LinearLayout llTop;
    @BindView(R.id.ll_information)
    LinearLayout llInformation;
    @BindView(R.id.ll_plan)
    LinearLayout llPlan;
    @BindView(R.id.ll_achievement)
    LinearLayout llAchievement;
    @BindView(R.id.ll_order)
    LinearLayout llOrder;
    @BindView(R.id.ll_teacher)
    LinearLayout llTeacher;
    @BindView(R.id.ll_setup)
    LinearLayout llSetup;
    User user;
    User.UserBean userBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //判断为空，为空就去加载布局，onCreateView在界面切换的时候会被多次调用,防止界面跳转回来的时候显示空白
        if (mViewRoot == null) {
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mViewRoot = inflater.inflate(R.layout.fragment_mine, null);
            unbinder = ButterKnife.bind(this, mViewRoot);
            initView();
        }

        unbinder = ButterKnife.bind(this, mViewRoot);
        return mViewRoot;


    }

    private void initView() {
        //AppConstants.clearAllCache(getActivity());
        OkGo.post(AppConstants.BASE_URL + "/user/userInfor.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, okhttp3.Call call, okhttp3.Response response) {
                        Log.e("user", s);
                        Gson gson = new Gson();
                        user = gson.fromJson(s, User.class);
                        if (user.getMsg().equals("success")) {
                            userBean = user.getUser();
                            Glide.with(getActivity())
                                    .load(AppConstants.IMG_URL + userBean.getHeader()).error(R.mipmap.tx)
                                    .into(ivFloatHeader);
                            tvFloatName.setText(userBean.getName());
                            tvMyPhone.setText(userBean.getMobile());

                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Toast.makeText(getActivity(), "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                    }
                });
        llInformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到个人信息界面
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), PersonalInformationActivity.class);
                        startActivity(intent);
                    }

                }
            }
        });
        llPlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        //学业规划
                        Intent intent = new Intent(getActivity(), AcademicPlanningActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
        llCumulative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        //累计上课
                        Intent intent = new Intent(getActivity(), StuCumulativeClassActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
        llOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的订单
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), OrderActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
        llTeacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的老师
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), MyTeacherActivity.class);
                        startActivity(intent);
                    }
                }

            }
        });
        coursePurchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //课程购买
                Intent intent = new Intent(getActivity(), CoursePurchaseActivity.class);
                startActivity(intent);

            }
        });
        llSetup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //我的设置
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), MySettingActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
        //我的成绩
        llAchievement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), MyGradeActivity.class);
                        startActivity(intent);
                    }
                }

            }
        });
        //点击头像修改个人信息
        ivFloatHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到个人信息界面
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(getActivity());
                    } else {
                        Intent intent = new Intent(getActivity(), PersonalInformationActivity.class);
                        startActivity(intent);
                    }

                }
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
